import React, { useEffect, useState } from "react";
import '../loan.css'
import { useHistory } from "react-router-dom/cjs/react-router-dom";
import { DotLoading, Mask } from "antd-mobile";
import { useDispatch } from "react-redux";
import loan_reject from "@/assets/loan_reject.png"
import { isLoanLoaning, isLoanReject, isLoanRepayNow, isLoanReviewing, isLoanSigning } from "@/utils/loanStatus";
import { PageTag } from "@/utils/pageTag";
import NavigateBar from "@/components/NavigateBar";
import AddHomeEvent from "@/utils/addHomeEvent";
import { reqSaveEvent, reqUserState } from "@/store/actions/apiServices";

/**
 * 审核中页
 */
export default function Reject() {

    //loading状态
    const [maskVisible, setMaskVisible] = useState(false)

    //显示add home弹框
    const [addHomeMaskVisible, setAddHomeMaskVisible] = useState(false);
    const [addHomeMaskIsHomeMode, setAddHomeMaskIsHomeMode] = useState(true);

    // 获取路由历史 history 对象
    const history = useHistory()

    //获取 Redux 分发器
    const dispatch = useDispatch()

    const onReqUserStateCallback = (success, resObj, pageTag) => {
        setMaskVisible(false);
        if (success && resObj) {
            //是否为签约中
            const isHomeSigning = isLoanSigning(resObj.signFlag, resObj.signOrderId);
            //是否为审核中
            const isHomeReviewing = isLoanReviewing(resObj.applyStatus);
            //是否为拒绝
            const isHomeReject = isLoanReject(resObj.applyStatus, resObj.orderStatus);
            //是否为放款中
            const isHomeLoaning = isLoanLoaning(resObj.orderStatus);
            //是否为待还款
            const isHomeRepay = isLoanRepayNow(resObj.orderStatus);
            //是否为待进件
            const isHomeNotLoan = ((!isHomeReviewing) && (!isHomeReject) && (!isHomeLoaning) && (!isHomeRepay));
            console.log("Launch-isHomeReviewing: " + isHomeReviewing);
            console.log("Launch-isHomeReject: " + isHomeReject);
            console.log("Launch-isHomeLoaning: " + isHomeLoaning);
            console.log("Launch-isHomeRepay: " + isHomeRepay);
            console.log("Launch-isHomeNotLoan: " + isHomeNotLoan);
            console.log("Launch-isHomeSigning: " + isHomeSigning);

            if (isHomeSigning) {
                history.replace('/guide');
            } else if (isHomeReviewing) {
                history.replace('/review');
            } else if (isHomeReject) {

            } else if (isHomeLoaning) {
                history.replace('/loaning');
            } else if (isHomeRepay) {
                history.replace('/repay');
            } else {
                history.replace('/home');
            }
        }
    };

    //点击提交
    const onHandleSubmit = () => {
        setMaskVisible(true);
        reqUserState(PageTag.REJECT, onReqUserStateCallback);
    }

    // 点击add home监听
    const onHandleAddToHome = (type) => {
        if (type === 'home') {
            AddHomeEvent.onHandleAddHomeEventClick()
        } else {
            AddHomeEvent.onHandleAddFavoriteEventClick()
        }
    }

    // 关闭add home
    const onHandleAddToHomeClose = () => {
        setAddHomeMaskVisible(false)
    }

    const onHandleAddHomeScreen = () => {
        setAddHomeMaskIsHomeMode(false);
        setAddHomeMaskVisible(false);
        history.push('/install');
    }

    useEffect(() => {
        reqSaveEvent("REJECT_START")

        return () => {
            reqSaveEvent("REJECT_END")
        }
    }, [])

    return (
        <div className="loanRoot mainBackgroundWhite">
            <NavigateBar theme="white" onAddFavorite={() => { onHandleAddHomeScreen(); }} onAddHome={() => { onHandleAddHomeScreen(); }}>Onua Credit</NavigateBar>

            <div hidden className="loanContent">
                <div className="statusImageContent">
                    <img
                        className="statusImage"
                        src={loan_reject}
                        alt="" />
                </div>

                <span className="statusTitle">Your application has not been approved</span>

                <span className="statusTips">please apply again 30 days later.</span>

                <div className="statusRefreshButtonContent" onClick={onHandleSubmit}>
                    <div className="mainButtonGradient">OK</div>
                </div>
            </div>

            {/* <AddToHomeGifDialog visible={addHomeMaskVisible} onConfirm={onHandleAddToHome} onClose={onHandleAddToHomeClose} isHomeMode={addHomeMaskIsHomeMode} /> */}

            <Mask visible={maskVisible} opacity={0.1}
                onMaskClick={() => { }}>
                <div className="overlayContent">
                    <span style={{ fontSize: 24, color: '#ff3141' }} >
                        <DotLoading color="currentColor" />
                    </span>
                </div>
            </Mask>
        </div >
    )
}